package com.njlime.cms.service.sys;

import com.njlime.cms.entities.CommunityComment;
import com.njlime.cms.repository.sys.CommunityCommentDao;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.sys.MemberService;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * Created by   on 2017/1/12.
 */
@Service
@Transactional(readOnly = true)
public class CommunityCommentService {
    @Resource
    CommunityCommentDao communityCommentDao;
    @Resource
    CommunityService communityService;
    @Resource
    private MemberService memberService;


    /**
     * 详情
     *
     * @param id
     * @return
     */
    public CommunityComment findOne(int id) {
        return communityCommentDao.findOne(id);
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Transactional(readOnly = false)
    public int save(CommunityComment info) {
        info.setCreate_time(new Date());
        info = communityCommentDao.save(info);
        return info.getId();
    }

    /**
     * 删除
     * @return
     */
    @Transactional(readOnly = false)
    public int del(CommunityComment comment) {
        int count = communityCommentDao.del(comment.getId());
        comment = communityCommentDao.findOne(comment.getId());
        if(comment.getStatus() == GlobalValue.AUDITED){
            communityService.updateCommentNum(comment.getCommunity_id(), -1);
        }
        return count;
    }

    /**
     * 分页列表
     *
     * @param pageBean
     * @param info
     * @return
     */
    public PageBean list(PageBean pageBean, CommunityComment info) {
        return communityCommentDao.list(pageBean, info);
    }

    /**
     * 点赞
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int like(int id) {
        return communityCommentDao.like(id);
    }

    /**
     * 评论置顶取消置顶
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int top(int id ){
        return communityCommentDao.top(id);
    }

    /**
     * 回复
     *
     * @param id
     * @param reply
     * @return
     */
    @Transactional(readOnly = false)
    public int reply(int id, String reply, Integer userId) {
        return communityCommentDao.reply(id, reply, userId);
    }

    /**
     * 审核
     *
     * @param communityComment
     */
    @Transactional(readOnly = false)
    public void auditComment(CommunityComment communityComment, MemberComment memberComment) {
        communityCommentDao.auditComment(communityComment.getId());
        memberService.saveComment(memberComment);
        communityService.updateCommentNum(communityComment.getCommunity_id(), 1);
    }

    /**
     * 奖励和取消奖励
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int commentReward(int id){
        return communityCommentDao.commentReward(id);
    }
}
